<?php
	class MsDAO{
		protected $db;
		
		public function __construct($db){
			$this->db = $db;
		}
		
		private function getDb(){
			return $this->db;
		}
		
		public function addMs($m){
			$sql = "insert into ms (descr, ref,
									ncm, marca, qtde,
									forn, classe, preco_venda, preco_plus,
									preco_vr, preco_custo, perc_st,
									perc_ipi, local, obs)					
					values (".($m->getDescr() ? "'{$m->getDescr()}'" : "null").",".($m->getRef() ? "'{$m->getRef()}'" : "null").",
							'{$m->getNcm()}','".(is_object($m->getMarca()) ? $m->getMarca()->getId() : "")."','{$m->getQtde()}','".(is_object($m->getForn()) ? $m->getForn()->getId() : "")."',
							'".(is_object($m->getClasse()) ? $m->getClasse()->getId() : "")."',
							'{$m->getPrecoVenda()}','{$m->getPrecoPlus()}','{$m->getPrecoVr()}',
							'{$m->getPrecoCusto()}','{$m->getPercSt()}','{$m->getPercIpi()}','{$m->getLocal()}','{$m->getObs()}')";

			$res = $this->getDb()->insert($sql);		
			return $res;
		}
		
		public function setMs($m){
			$sql = "update ms set 
						descr = ".($m->getDescr() ? "'{$m->getDescr()}'" : "null").",
						ref = ".($m->getRef() ? "'{$m->getRef()}'" : "null").",						
						ncm = '{$m->getNcm()}',
						marca = '".(is_object($m->getMarca()) ? $m->getMarca()->getId() : "")."',
						qtde = '{$m->getQtde()}',
						forn = '".(is_object($m->getForn()) ? $m->getForn()->getId() : "")."',						
						classe = '".(is_object($m->getClasse()) ? $m->getClasse()->getId() : "")."',						
						preco_venda = '{$m->getPrecoVenda()}',
						preco_plus = '{$m->getPrecoPlus()}',
						preco_vr = '{$m->getPrecoVr()}',
						preco_custo = '{$m->getPrecoCusto()}',
						perc_ipi = '{$m->getPercIpi()}',
						perc_st = '{$m->getPercSt()}',
						local = '{$m->getLocal()}',
						obs = '{$m->getObs()}'
					where ms = {$m->getId()}";		
			$res = $this->getDb()->query($sql);
			return $res;
		}

		public function getMs($id){
			$sql = "select 
						MS, DESCR, REF, UN,
						NCM, MARCA, QTDE,
						FORN, CLASSE, PRECO_VENDA, PRECO_PLUS,
						PRECO_VR, PRECO_CUSTO, PERC_ST,
						PERC_IPI, LOCAL, OBS
					from ms
					where ms = {$id}
					order by descr, marca";
			$res = $this->getDb()->query($sql);
			$ret = $res[0];			

			$mad = new MarcaDAO($this->getDb());
			$fod = new FornDAO($this->getDb());
			$cld = new ClasseDAO($this->getDb());

			$ms = new Ms($ret['MS'],$ret['DESCR'],$ret['REF'],$ret['UN'],$ret['NCM'],
							 $mad->getMarca($ret['MARCA']),$ret['QTDE'],$fod->getForn($ret['FORN']),$cld->getClasse($ret['CLASSE']),$ret['PRECO_VENDA'],
							 $ret['PRECO_PLUS'],$ret['PRECO_VR'],$ret['PRECO_CUSTO'],
							 $ret['PERC_ST'],$ret['PERC_IPI'],'',$ret['OBS']);			
			return $ms;							 	
		}

		public function getLista($where=array()){
			$where = ($where ? "where ".implode(" and ", $where) : "");
			$sql = "select 
						MS, DESCR, REF, UN
						NCM, MARCA, QTDE,
						FORN, CLASSE, PRECO_VENDA, PRECO_PLUS,
						PRECO_VR, PRECO_CUSTO, PERC_ST,
						PERC_IPI, LOCAL, OBS
					from ms
					{$where}
					order by descr, marca";			
			$res = $this->getDb()->query($sql);		

			$listaMs = array();
			if($res){
				$mad = new MarcaDAO($this->getDb());
				$fod = new FornDAO($this->getDb());
				$cld = new ClasseDAO($this->getDb());
				foreach($res as $ret){

					$listaMs[] = new Ms($ret['MS'],$ret['DESCR'],$ret['REF'],$ret['UN'],$ret['NCM'],
										 $mad->getMarca($ret['MARCA']),$ret['QTDE'],$fod->getForn($ret['FORN']),$cld->getClasse($ret['CLASSE']),number_format($ret['PRECO_VENDA'],2,',','.'),
							 number_format($ret['PRECO_PLUS'],2,',','.'),number_format($ret['PRECO_VR'],2,',','.'),number_format($ret['PRECO_CUSTO'],2,',','.'),
							 number_format($ret['PERC_ST'],2,',','.'),number_format($ret['PERC_IPI'],2,',','.'),'',$ret['OBS']);	
				}
			}
			return $listaMs;
		}
	}
?>